﻿
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_Ads_AddAds : System.Web.UI.Page
{
    string Fname;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        if (!IsPostBack)
        {
           
            try
            {
                ddlKindNo.SelectedItem.Selected = false;
                ddlKindNo.Items.FindByValue(Request.QueryString["KindNo"]).Selected = true;
            }
            catch { }
            BindData();

        }
    }



    private void BindData()
    {
        if (Request.QueryString["AdsID"] != "" && Request.QueryString["AdsID"] != null)
        {           
            clsWebShop cls = new clsWebShop();

            PccMsg msg = new PccMsg();
            msg.CreateFirstNode("AdsID", Request.QueryString["AdsID"]);
            msg.CreateFirstNode("KindNo",ddlKindNo.SelectedValue);

            DataTable dt = cls.Get_AdsByCondition(msg.GetXmlStr.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                txtName.Text = dt.Rows[0]["AdsNm"].ToString();
                txtTheLoai.Text = dt.Rows[0]["AdsLink"].ToString();
                txtDesc.Text = dt.Rows[0]["AdsDes"].ToString();

               

                string sURL = dt.Rows[0]["AdsURL"].ToString();
                
                if (sURL != "" && sURL != null)
                {
                    imgShowImage.Visible = true;
                    hImg.Value = sURL;
                    if (dt.Rows[0]["AdsType"].ToString().ToUpper() == "IMG")
                    {
                        HtmlAnchor embed = new HtmlAnchor();
                        embed.InnerHtml = "<img border='0' src='" + ResolveUrl("~/" + dt.Rows[0]["AdsURL"].ToString().ToUpper()) + "'  >";
                        tdControlFile.Controls.Add(embed);
                    }
                    else if (dt.Rows[0]["AdsType"].ToString().ToUpper() == "SWF")
                    {
                        HtmlAnchor embed = new HtmlAnchor();
                        embed.InnerHtml = "<embed quality='high'   src='" + ResolveUrl("~/" + dt.Rows[0]["AdsURL"].ToString().ToUpper()) + "'  loop='false'  pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'     > </embed>";
                        tdControlFile.Controls.Add(embed);
                    }

                   /* imgShowImage.Visible = true;
                    imgShowImage.Style.Add("cursor", "pointer");
                    imgURL.Src = "../../" + sURL;
                    hImg.Value = sURL;*/
                }


            }
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string path = "Images/";
        string type = "IMG";
        if (ddlKindNo.SelectedValue == "A")
            path += "Ads/";
        else
            path += "Partner/";
        if (FileU_HinhMinhHoa.HasFile)
        {

            if (System.IO.Path.GetExtension(FileU_HinhMinhHoa.FileName.ToLower()) == ".swf")
            {
                type = "SWF";
            }
            else
            {
                System.Text.RegularExpressions.Regex objRegex = new System.Text.RegularExpressions.Regex(@"(.*?)\.jpg|(.*?)\.png|(.*?)\.gif|(.*?)\.bmp");
                if (!objRegex.IsMatch(FileU_HinhMinhHoa.FileName.ToLower()))
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Xin vui lòng chọn file hình ảnh')</script>");
                    return;
                }
            }


            System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath("~") + "/" + path + hImg.Value);
            if (file.Exists)
                file.Delete();


            string FPath, strdate;
            strdate = System.DateTime.Today.ToString("yyyyMMddhhmmss");
            Fname = path + strdate + FileU_HinhMinhHoa.FileName;
            FPath = Server.MapPath("~/") + Fname;
            FileU_HinhMinhHoa.SaveAs(FPath);


        }
        else // Giu lai file cu
        {
            Fname = hImg.Value;
            if (System.IO.Path.GetExtension(hImg.Value.ToLower()) == ".swf")
            {
                type = "SWF";
            }
        }

        clsWebShop cls = new clsWebShop();



        string ID = "0";
        if (Request.QueryString["AdsID"] != "" && Request.QueryString["AdsID"] != null)
        {
            ID = Request.QueryString["AdsID"];
        }
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("AdsID", ID);
        msg.CreateFirstNode("KindNo", ddlKindNo.SelectedValue);
        msg.CreateFirstNode("AdsNm", txtName.Text);
        msg.CreateFirstNode("AdsDes", txtDesc.Text);
        msg.CreateFirstNode("AdsLink", txtTheLoai.Text);
        msg.CreateFirstNode("AdsURL", Fname);
        msg.CreateFirstNode("AdsType", type);   
        msg.CreateFirstNode("UpdID", Session["AdminUserID"].ToString());

        cls.Ins_Upd_Ads(msg.GetXmlStr.ToString());

        ReturnPage();
        //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script> alert('Dữ liệu đã cập nhật thành công');  window.location.href='Product.aspx'</script>");
    }

    private void ReturnPage()
    {
        Response.Redirect("Ads.aspx?KindNo="+Request.QueryString["KindNo"]+"&CodeID=" + Request.QueryString["CodeID"]);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ReturnPage();
    }
}
